"""
带外管理模块诊断脚本
运行此脚本来诊断带外管理模块无法加载的问题
"""

import sys
import os
import traceback

# 添加项目根目录到路径
project_root = os.path.join(os.path.dirname(__file__))
sys.path.insert(0, project_root)

def diagnose_oob_module():
    """诊断带外管理模块"""
    print("=" * 60)
    print("带外管理模块诊断")
    print("=" * 60)
    
    # 1. 检查文件是否存在
    print("\n1. 检查文件结构...")
    required_files = [
        "src/app/modules/oob/__init__.py",
        "src/app/modules/oob/utils.py",
        "src/app/modules/oob/cache.py",
        "src/app/modules/oob/service.py",
        "src/app/modules/oob/ui/__init__.py",
        "src/app/modules/oob/ui/oob_dialog.py",
        "src/app/modules/oob/ui/oob_widget.py"
    ]
    
    all_files_exist = True
    for file_path in required_files:
        full_path = os.path.join(project_root, file_path)
        if os.path.exists(full_path):
            print(f"   ✓ {file_path}")
        else:
            print(f"   ✗ {file_path} - 文件不存在")
            all_files_exist = False
    
    if not all_files_exist:
        print("\n❌ 部分必需文件不存在，请检查文件结构")
        return False
    
    # 2. 测试导入
    print("\n2. 测试模块导入...")
    
    try:
        print("   测试导入OobUtils...")
        from src.app.modules.oob.utils import OobUtils
        print("   ✓ OobUtils导入成功")
    except Exception as e:
        print(f"   ✗ OobUtils导入失败: {e}")
        traceback.print_exc()
        return False
    
    try:
        print("   测试导入OobCache...")
        from src.app.modules.oob.cache import OobCache
        print("   ✓ OobCache导入成功")
    except Exception as e:
        print(f"   ✗ OobCache导入失败: {e}")
        traceback.print_exc()
        return False
    
    try:
        print("   测试导入OobService...")
        from src.app.modules.oob.service import OobService
        print("   ✓ OobService导入成功")
    except Exception as e:
        print(f"   ✗ OobService导入失败: {e}")
        traceback.print_exc()
        return False
    
    try:
        print("   测试导入OobDialog...")
        from src.app.modules.oob.ui.oob_dialog import OobDialog
        print("   ✓ OobDialog导入成功")
    except Exception as e:
        print(f"   ✗ OobDialog导入失败: {e}")
        traceback.print_exc()
        return False
    
    try:
        print("   测试导入OobWidget...")
        from src.app.modules.oob.ui.oob_widget import OobWidget
        print("   ✓ OobWidget导入成功")
    except Exception as e:
        print(f"   ✗ OobWidget导入失败: {e}")
        traceback.print_exc()
        return False
    
    # 3. 测试实例化
    print("\n3. 测试组件实例化...")
    
    try:
        print("   测试OobUtils实例化...")
        utils = OobUtils()
        print("   ✓ OobUtils实例化成功")
    except Exception as e:
        print(f"   ✗ OobUtils实例化失败: {e}")
        traceback.print_exc()
        return False
    
    try:
        print("   测试OobCache实例化...")
        cache = OobCache()
        print("   ✓ OobCache实例化成功")
    except Exception as e:
        print(f"   ✗ OobCache实例化失败: {e}")
        traceback.print_exc()
        return False
    
    # 4. 测试主窗口导入
    print("\n4. 测试主窗口中的导入...")
    
    try:
        print("   测试主窗口导入OobWidget...")
        # 模拟主窗口中的导入
        from src.app.modules.oob.ui.oob_widget import OobWidget
        print("   ✓ 主窗口导入OobWidget成功")
    except Exception as e:
        print(f"   ✗ 主窗口导入OobWidget失败: {e}")
        traceback.print_exc()
        return False
    
    print("\n✅ 所有诊断测试通过！带外管理模块应该可以正常加载。")
    return True

def suggest_solutions():
    """提供解决方案建议"""
    print("\n" + "=" * 60)
    print("解决方案建议")
    print("=" * 60)
    
    print("\n如果带外管理模块仍然无法加载，请尝试以下解决方案：")
    print("\n1. 确保Python环境正确：")
    print("   - 激活虚拟环境：.venv\\Scripts\\activate")
    print("   - 安装依赖：pip install -r requirements.txt")
    
    print("\n2. 检查PyQt5安装：")
    print("   - 验证安装：python -c \"from PyQt5.QtWidgets import QApplication\"")
    print("   - 重新安装：pip install PyQt5==5.15.10")
    
    print("\n3. 检查数据库连接：")
    print("   - 确保itts.db文件存在")
    print("   - 运行数据库测试：python test_database.py")
    
    print("\n4. 清理Python缓存：")
    print("   - 删除__pycache__目录")
    print("   - 删除.pyc文件")
    
    print("\n5. 重新启动应用程序：")
    print("   - 完全关闭应用程序")
    print("   - 重新运行：python src/main.py")
    
    print("\n6. 查看详细错误日志：")
    print("   - 检查itts.log文件")
    print("   - 查看控制台输出")

if __name__ == '__main__':
    success = diagnose_oob_module()
    
    if not success:
        suggest_solutions()
    else:
        print("\n🎉 诊断完成，模块应该可以正常工作！")
        print("\n如果问题仍然存在，请运行应用程序并查看控制台输出的详细错误信息。")